package com.ruoyi.lowcode.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.lowcode.domain.LcDirectoryMenu;
import com.ruoyi.lowcode.mapper.LcDirectoryMenuMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.lowcode.domain.bo.LcDirectoryBo;
import com.ruoyi.lowcode.domain.vo.LcDirectoryVo;
import com.ruoyi.lowcode.domain.LcDirectory;
import com.ruoyi.lowcode.mapper.LcDirectoryMapper;
import com.ruoyi.lowcode.service.ILcDirectoryService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 目录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-04
 */
@RequiredArgsConstructor
@Service
public class LcDirectoryServiceImpl implements ILcDirectoryService {

    private final LcDirectoryMapper baseMapper;

    private final LcDirectoryMenuMapper directoryMenuMapper;

    /**
     * 查询目录
     */
    @Override
    public LcDirectoryVo queryById(Long directoryId){
        return baseMapper.selectVoById(directoryId);
    }

    /**
     * 查询目录列表
     */
    @Override
    public TableDataInfo<LcDirectoryVo> queryPageList(LcDirectoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LcDirectory> lqw = buildQueryWrapper(bo);
        Page<LcDirectoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询目录列表
     */
    @Override
    public List<LcDirectoryVo> queryList(LcDirectoryBo bo) {
        LambdaQueryWrapper<LcDirectory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LcDirectory> buildQueryWrapper(LcDirectoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LcDirectory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDirectoryName()), LcDirectory::getDirectoryName, bo.getDirectoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), LcDirectory::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDefault()), LcDirectory::getIsDefault, bo.getIsDefault());
        return lqw;
    }

    /**
     * 新增目录
     */
    @Transactional
    @Override
    public Boolean insertByBo(LcDirectoryBo bo) {
        LcDirectory add = BeanUtil.toBean(bo, LcDirectory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDirectoryId(add.getDirectoryId());

            LcDirectoryMenu directoryMenu = new LcDirectoryMenu();
            directoryMenu.setDirectoryId(add.getDirectoryId());
            directoryMenu.setMenuName(add.getDirectoryName());
            directoryMenu.setAncestors("0");
            directoryMenuMapper.insert(directoryMenu);

        }
        return flag;
    }

    /**
     * 修改目录
     */
    @Override
    public Boolean updateByBo(LcDirectoryBo bo) {
        LcDirectory update = BeanUtil.toBean(bo, LcDirectory.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if(flag){
             LcDirectoryMenu directoryMenu = directoryMenuMapper.selectOne(
                    new QueryWrapper<LcDirectoryMenu>()
                            .eq("directory_id", bo.getDirectoryId())
                            .eq("parent_id", 0)
                            .eq("del_flag", "0")
             );
             if(ObjectUtil.isNotNull(directoryMenu)){
                 directoryMenu.setMenuName(bo.getDirectoryName());
                 directoryMenuMapper.updateById(directoryMenu);
             }
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LcDirectory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除目录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
